#include <cstdlib>
#include <iostream>
#include <vector>
#include "structs.h"
#include "Tower.h"

Tower::Tower(vector<int> disks,int number) {
    this->Disks = disks;
    this->size = disks.size();
    this->number = number;
}

Tower::Tower() {  
}

Tower::~Tower() {
    this->Disks.clear();
//    delete[] this;
}

void Tower::clear() {
    this->Disks.clear();
}

void Tower::print() {
    cout << "Tower" << this->number << " : "; 
    for(int i=0;i<this->Disks.size();i++) {
        cout << this->Disks.at(i) << " , ";        
    }
    cout << endl;
}

void Tower::print(ostream& out) {
    out << "Tower" << this->number << " : "; 
    for(int i=0;i<this->Disks.size();i++) {
        out << this->Disks.at(i) << " , ";        
    }
    out << endl;
}

bool Tower::add(int disk) {
    if (this->lastDisc() < disk ) { return false; }
    else {
        this->Disks.push_back(disk);
        return true;
    }
}

void Tower::remove() {
//    cout << "           SIZE BEFORE " << this->Disks.size() << endl;
    
    this->Disks.pop_back();
//    cout << "           SIZE AFTER " << this->Disks.size() << endl;
}

bool Tower::isEmpty() {
    if (this->Disks.size()==0) return true;
    else return false;
}

bool Tower::isComplete(int n) {
    if (this->Disks.size() != n) return false;
    else {
        for(int i=0;i<n;i++) if (this->Disks.at(i) != n-i) return false;
    }
    return true;
}

void Tower::decompose(int* a) {
    for(int i=0;i<Disks.size();i++) {
//        cout << a[Disks.at(i)-1] << endl;
        a[Disks.at(i)-1] = number;
//        cout << a[Disks.at(i)-1] << endl;
    }
}

int Tower::lastDisc() {
    //Vraci vrchni disk veze, kdyz je vez prazdna vraci max integer
    if ( this->Disks.size() == 0 ) return 32767;
    return this->Disks.back();
}

